package com.example.hcd.functionactivity;

import java.util.ArrayList;
import java.util.List;

import com.example.hcd.R;
import com.example.hcd.tool.KCalendar;


import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SignInfoActivity extends BaseActivity{
	private TextView title;

	String date = null;// 设置默认选中的日期  格式为 “2014-04-05” 标准DATE格式
	
	private String userId;
	
	private String name;


	public SignInfoActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_info_view);
		mPageName = "SignInfoActivity";
		ImageView back = (ImageView)findViewById(R.id.title_bar_btn_left);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		userId = getIntent().getStringExtra("USERID");
		name = getIntent().getStringExtra("USERNAME");

		//
		title = (TextView)findViewById(R.id.title_content);
		title.setText(name+"签到详情");
		//初始化日历
		final TextView popupwindow_calendar_month = (TextView) findViewById(R.id.popupwindow_calendar_month);
		final KCalendar calendar = (KCalendar)findViewById(R.id.popupwindow_calendar);
		calendar.setIsShowInfo(true, userId);
		popupwindow_calendar_month.setText(calendar.getCalendarYear() + "年"
				+ calendar.getCalendarMonth() + "月");

		if (null != date) {

			int years = Integer.parseInt(date.substring(0,
					date.indexOf("-")));
			int month = Integer.parseInt(date.substring(
					date.indexOf("-") + 1, date.lastIndexOf("-")));
			popupwindow_calendar_month.setText(years + "年" + month + "月");

			calendar.showCalendar(years, month);
			calendar.setCalendarDayBgColor(date,
					R.drawable.calendar_date_focused);
		}

		List<String> list = new ArrayList<String>(); //设置标记列表
		list.add("2014-04-01");
		list.add("2014-04-02");
		calendar.addMarks(list, 0);

		//监听所选中的日期
		calendar.setOnCalendarClickListener(new KCalendar.OnCalendarClickListener() {

			public void onCalendarClick(int row, int col, String dateFormat) {
				int month = Integer.parseInt(dateFormat.substring(
						dateFormat.indexOf("-") + 1,
						dateFormat.lastIndexOf("-")));

				if (calendar.getCalendarMonth() - month == 1//跨年跳转
						|| calendar.getCalendarMonth() - month == -11) {
					calendar.lastMonth();

				} else if (month - calendar.getCalendarMonth() == 1 //跨年跳转
						|| month - calendar.getCalendarMonth() == -11) {
					calendar.nextMonth();

				} else {
					//calendar.removeAllBgColor();
//					calendar.setCalendarDayBgColor(dateFormat,
//							R.drawable.calendar_date_focused);
					date = dateFormat;//最后返回给全局 date
				}
			}
		});

		//监听当前月份
		calendar.setOnCalendarDateChangedListener(new KCalendar.OnCalendarDateChangedListener() {
			public void onCalendarDateChanged(int year, int month) {
				popupwindow_calendar_month
						.setText(year + "年" + month + "月");
			}
		});

		//上月监听按钮
		RelativeLayout popupwindow_calendar_last_month = (RelativeLayout)findViewById(R.id.popupwindow_calendar_last_month);
		popupwindow_calendar_last_month
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						calendar.lastMonth();
						//更新签到详情
						calendar.updateList(SignInfoActivity.this);
					}

				});

		//下月监听按钮
		RelativeLayout popupwindow_calendar_next_month = (RelativeLayout)findViewById(R.id.popupwindow_calendar_next_month);
		popupwindow_calendar_next_month
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						calendar.nextMonth();
						//更新签到详情
						calendar.updateList(SignInfoActivity.this);
					}
				});
		//更新签到详情
		calendar.updateList(this);

	}

}
